import * as globby from "globby";
import * as commander from "commander";
import * as path from "path";
import { error, chalk, fs, info } from "./lib";
import * as pacote from "pacote";
import { parseWithoutProcessing } from "handlebars";
const { program } = commander;
let commandsPath = [];
let pkgVersion = "";
let pkgName = "";

// 获取src/command路径下的命令
const getCommand = () => {
  commandsPath =
    (globby as any).sync("./commands/*.*s", {
      cwd: __dirname,
      deep: 1,
    }) || [];

  return commandsPath;
};

// 获取当前包的信息
const getPkgInfo = () => {
  const jsonPath = path.join(__dirname, "../package.json");
  const jsonContent = fs.readFileSync(jsonPath, "utf-8");
  const jsonResult = JSON.parse(jsonContent);
  pkgVersion = jsonResult.version;
  pkgName = jsonResult.name;
};

// 获取最新包的最新版本
const getLatestVersion = async () => {
  const mainfest = await pacote.manifest(`${pkgName}@latest`);
  return mainfest.version;
};

function start() {
  //   getPkgInfo();
  const commandsPath = getCommand();
  //   program.version(pkgVersion);
  commandsPath.forEach((commandPath) => {
    const commandObj = require(`./${commandPath}`);
    const { command, desciption, optionList, action } = commandObj.default;
    const curp = program
      .command(command)
      .description(desciption)
      .action(action);

    optionList &&
      optionList.map((option: [string]) => {
        curp.option(...option);
      });
  });
  program.on("command:*", async ([cmd]) => {
    program.outputHelp();
    error(`未知命令 command ${chalk.yellow(cmd)}.`);
    const latestVersion = await getLatestVersion();
    if (latestVersion !== pkgVersion) {
      info(
        `可更新版本，${chalk.green(pkgVersion)} -> ${chalk.green(
          latestVersion
        )}`
      );
    }
    process.exitCode = 1;
  });
  program.parseAsync(process.argv);
}

start();
